<template>
  <div id="globalHeader">
    <!--  :wrap="false" 不允许换行  -->
    <a-row :wrap="false">
      <a-col flex="200px">
        <RouterLink to="/">
          <div class="title-bar">
            <img class="logo" src="../assets/logo.png" alt="logo" />
            <div class="title">智能云图库</div>
          </div>
        </RouterLink>
      </a-col>
      <a-col flex="auto">
        <a-menu
          v-model:selectedKeys="current"
          mode="horizontal"
          :items="menus"
          @click="doMenuClick"
          style='font-size: 16px;background: #F3F8FE'
        />
      </a-col>
      <a-col flex="120px">
        <div class="user-login-status">
          <div v-if="loginUserStore.loginUser.id">
            <a-dropdown>
              <a-space>
                <a-avatar :src="loginUserStore.loginUser.userAvatar" />
                {{ loginUserStore.loginUser.userName }}
              </a-space>
              <template #overlay>
                <a-menu>
                  <a-menu-item key="2" @click="addImageClick">
                    <a-space><FileAddOutlined />新增图片</a-space>
                  </a-menu-item>
                  <a-menu-item>
                    <router-link to="/space/add">
                      <a-space><UserOutlined />创建私人空间</a-space>
                    </router-link>
                  </a-menu-item>
                  <a-menu-item>
                    <router-link to="/space/add?type=1">
                      <a-space><TeamOutlined />创建团队空间</a-space>
                    </router-link>
                  </a-menu-item>
                  <a-menu-item key="0" @click="showDrawer">
                    <a-space><SettingOutlined />个人设置</a-space>
                  </a-menu-item>
                  <a-menu-item key="1" @click="doLogoutClick">
                    <a-space><LogoutOutlined />退出登录</a-space>
                  </a-menu-item>
                </a-menu>
              </template>
            </a-dropdown>
          </div>
          <div v-else>
            <a-button type="primary" href="/user/login">登录</a-button>
          </div>
        </div>
      </a-col>
    </a-row>
  </div>
</template>

<script lang="ts" setup>
import { computed, ref } from 'vue'
import { LogoutOutlined, SettingOutlined, UserOutlined, FileAddOutlined,TeamOutlined } from '@ant-design/icons-vue'
import { MenuProps, message } from 'ant-design-vue'
import { useRouter } from 'vue-router'
import { useLoginUserStore } from '@/stores/useLoginUserStore.ts'
import { logoutUserUsingPost } from '@/api/userController.ts'
import checkAccess from '@/access/checkAccess.ts'
// 用于标识高亮
const current = ref<string[]>([])
const loginUserStore = useLoginUserStore()
// 把路由项转换为菜单项
const toMenuItem = (item: any) => {
  return {
    key: item.path,
    label: item.name,
    title: item.name,
    icon: item.meta?.icon ?? undefined,
  }
}
const router = useRouter()
// 过滤菜单项
const items = computed(() => {
  return router
    .getRoutes()
    .toReversed()
    .filter((item) => {
      if (item.meta?.hideInMenu) {
        return false
      }
      // 根据权限过滤菜单，有权限则返回 true，则保留该菜单
      return checkAccess(loginUserStore.loginUser, item.meta?.access as string)
    })
    .map(toMenuItem) // 转换为菜单项格式
})
// 导航栏
const menus = ref<MenuProps['items']>(items)

// 路由跳转事件
const doMenuClick = ({ key }: { key: string }) => {
  router.push({
    path: key,
  })
}
// 监听路由变化，更新当前选中菜单高亮
router.afterEach((to, from, next) => {
  current.value = [to.path]
})

const open = ref<boolean>(false)
// 打开抽屉组件
const showDrawer = () => {
  open.value = true
}
// 关闭抽屉组件
const closeDrawer = (e: MouseEvent) => {
  open.value = false
}
// 个人信息编辑
const doEditProfileClick = () => {}

// 账号登出
const doLogoutClick = async () => {
  const res = await logoutUserUsingPost()
  if (res.data.code === 0) {
    message.success('账号登出成功')
    // 清除登录状态
    loginUserStore.setLoginUser({
      userName: '未登录',
    })
    // 跳转到登录页面
    router.push({
      path: '/user/login',
      replace: true,
    })
  } else {
    message.error('账号登出失败，错误原因：' + res.data.message)
  }
}

const addImageClick = async () => {
  router.push({
    path: '/image/add',
  })
}
</script>

<style scoped>
.title-bar {
  display: flex;
  align-items: center;
}

.title {
  color: black;
  font-size: 22px;
  margin-left: 16px;
}

.logo {
  height: 48px;
}
</style>
